package com.smart.carey.util;

import com.smart.carey.exception.PaginationException;

import java.io.Serializable;
import java.util.Map;

/**
 * 分页工具类
 *
 * @author Carey
 */
public final class PaginationModel implements Serializable {

    private int pageSize;        //每页显示数据数
    private int totalCount;        //数据总数
    private int totalPage;        //总页数
    private int currentPage;    //当前页数
    private String url;        //跳转URL
    private String param;  //入参 json
    private Object data;


    public PaginationModel() {

    }

    public PaginationModel(Builder builder) {
        this.currentPage = builder.currentPage;
        this.data = builder.data;
        this.pageSize = builder.pageSize;
        this.totalCount = builder.totalCount;
        this.totalPage = builder.totalPage;
    }

    public String getParam() {
        return param;
    }

    public void setParam(String param) {
        this.param = param;
    }

    public int getPageSize() {
        return pageSize;
    }

    public int getTotalCount() {
        return totalCount;
    }

    public int getTotalPage() {
        return totalPage;
    }

    public int getCurrentPage() {
        return currentPage;
    }

    public String getUrl() {
        return url;
    }

    //线程不安全方法
    public void setUrl(String url) {
        this.url = url;
    }

    public Object getData() {
        return data;
    }

    public static class Builder {

        private int pageSize = 5;        //每页显示数据数
        private int currentPage = 1;    //当前页数
        private int totalCount = 0;        //数据总数
        private int totalPage = 0;        //总页数
        private Object data;


        public Builder(int totalCount) {
            this.totalCount = totalCount;
            this.totalPage = totalCount % pageSize == 0 ? totalCount / pageSize : totalCount / pageSize + 1;
        }


        public Builder setPageSize(Map<String, Object> map) throws PaginationException {
            Object objectPageSize = map.get("pageSize");
            if (null != objectPageSize) {
                if (objectPageSize instanceof Integer) {
                    this.pageSize = Integer.parseInt(objectPageSize.toString());
                }
            } else {
                throw new PaginationException("PageSize must be an integer!!");
            }

            return this;
        }

        public Builder setCurrentPage(Map<String, Object> map) throws PaginationException {
            Object objectCurrentPage = map.get("currentPage");
            if (null != objectCurrentPage) {
                if (objectCurrentPage instanceof Integer) {
                    this.currentPage = Integer.parseInt(objectCurrentPage.toString());
                }
            } else {
                throw new PaginationException("CurrentPage must be an integer!!");
            }
            return this;

        }

        public Builder setData(Object data) {
            this.data = data;
            return this;
        }


        public PaginationModel build() {

//            if(pageSize !=10){
            this.totalPage = totalCount % pageSize == 0 ? totalCount / pageSize : totalCount / pageSize + 1;
//            }

            return new PaginationModel(this);
        }

    }
}
